#include "StompedReality/Components/RenderInfoComponent.h"
#include "StompedReality/Core/ComponentConstuctionHelper.h"
#include "StompedReality/Core/Logger.h"
namespace StompedReality
{

namespace Core
{

ComponentConstuctionHelper::ComponentConstuctionHelper ( JsonPTR ptr )
  : StompedReality::GameAccess( )
{
  this->m_JsonPtr = ptr;
}

ComponentConstuctionHelper::~ComponentConstuctionHelper ( )
{
}

void ComponentConstuctionHelper::CreateComponents ( )
{
  assert ( this->m_Game != 0 );
  LogCallFunctionInfo ( "ComponentConstuctionHelper", "CreateComponents" );
  Json::Value cmpList = this->m_JsonPtr->get( "Components", "" );
  StompedReality::TYPE_ID renderInfoType = StompedReality::type_system::TypeSystem::GetTypeID( "RenderInfoComponent" );

  for ( unsigned int i = 0; i < cmpList.size( ); i++ )
  {
    LogCallFunctionSubInfo( "Create Component: " );
    LogInfoPair( "type", cmpList[i].get( "type", "" ).asCString( ));
    LogInfoPair( "name", cmpList[i].get( "name", "no name" ).asCString( ))

    // get Type, create object, read fields
    StompedReality::type_system::TypeInfo* typeInfo = StompedReality::type_system::TypeSystem::GetTypeByName (
        cmpList[i]["type"].asCString( ) );

    StompedReality::ID cmpID = this->m_Game->getComponentSystem( )->CreateObject ( typeInfo->typeID );
    StompedReality::Core::Component* cmp = this->m_Game->getComponentSystem( )->GetObject( typeInfo->typeID, cmpID );
    typeInfo->read( cmpList[i], (char*)cmp );

    // add new componet to temp list and test render info
    this->m_ComponentList.push_back( cmp );
    if ( typeInfo->typeID == renderInfoType )
    {
      this->m_RenderComponent = (StompedReality::components::RenderInfoComponent*) cmp;
    }
  }
  this->m_CurrentIDX = 0;
  for (ComponentList::iterator iter = this->m_ComponentList.begin( );
       iter != this->m_ComponentList.end( );
       iter++)
  {
    (*iter)->InitializeComponent ( this );
    this->m_CurrentIDX++;
  }
  this->m_RenderComponent->SortRenderFunctions ( );
}

int ComponentConstuctionHelper::FindCmpByName( std::string name )
{
  int idx = -1;
  for ( unsigned int i = 0; i < this->m_JsonPtr->get( "Components", "" ).size( ); i++ )
  {
    if ( this->m_JsonPtr->get( "Components", "" ).get( i, "" ).get( "name", "" ).asString( ).compare( name ) == 0 )
    {
      idx = i;
      break;
    }
  }
  return idx;
}

void ComponentConstuctionHelper::AddRenderFunction( StompedReality::Graphics::RenderFunction* function )
{
  this->m_RenderComponent->AddRenderFunction( function );
}

Json::Value ComponentConstuctionHelper::GetCurrentJsonValue( )
{
  return this->m_JsonPtr->get( "Components", "" )[this->m_CurrentIDX];
}

bool ComponentConstuctionHelper::IsStaticRenderable( )
{
  return this->GetCurrentJsonValue( ).get( "static", false ).asBool( );
}

void ComponentConstuctionHelper::Finish ( )
{
  this->m_RenderComponent->SortRenderFunctions ( );
}

StompedReality::ID ComponentConstuctionHelper::GetRenderInfoID( )
{
  return this->m_RenderComponent->GetID( );
}

}

}
